package com.example.smartcity_poverty.ui.AllService.RubbishClassify.activity

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import androidx.recyclerview.widget.GridLayoutManager
import com.example.smartcity_poverty.App.Companion.getToken
import com.example.smartcity_poverty.App.Companion.url
import com.example.smartcity_poverty.Base.BaseRecyclerAdapter
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.Repository.coroutine
import com.example.smartcity_poverty.logic.model.RubbishClassify.RcClassifyAllModel
import com.example.smartcity_poverty.logic.model.RubbishClassify.RcClassifyBanner
import com.example.smartcity_poverty.logic.model.RubbishClassify.RcSeekAllTitleModel
import com.example.smartcity_poverty.logic.model.RubbishClassify.RcSeekClassifyMpdel
import com.example.smartcity_poverty.logic.util.Util.glide
import com.example.smartcity_poverty.logic.util.Util.show
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import kotlinx.android.synthetic.main.activity_rc_classify.*
import kotlinx.android.synthetic.main.activity_seek_show_list.*
import kotlinx.android.synthetic.main.re_rc_seek_classify.*
import kotlinx.android.synthetic.main.re_rc_seek_show_all_title.*
import kotlinx.coroutines.Dispatchers

class RcClassifyActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_rc_classify)
        title = "分类"
        rcClassifyToolbar.setNavigationOnClickListener { finish() }

        coroutine(Dispatchers.IO) {
            // 海报
            setClassifyBanner()
            // 常见分类的垃圾
            setRcClassifyAll()
        }

        // 点击进入垃圾详情页
        rcClassifyBtn1.setOnClickListener {
            startActivity(
                Intent(this, RcShowClassifyActivity::class.java).putExtra(
                    "content",
                    rcClassifyBtn1.text.toString()
                )
            )
        }

        rcClassifyBtn2.setOnClickListener {
            startActivity(
                Intent(this, RcShowClassifyActivity::class.java).putExtra(
                    "content",
                    rcClassifyBtn2.text.toString()
                )
            )
        }

        rcClassifyBtn3.setOnClickListener {
            startActivity(
                Intent(this, RcShowClassifyActivity::class.java).putExtra(
                    "content",
                    rcClassifyBtn3.text.toString()
                )
            )
        }

        rcClassifyBtn4.setOnClickListener {
            startActivity(
                Intent(this, RcShowClassifyActivity::class.java).putExtra(
                    "content",
                    rcClassifyBtn4.text.toString()
                )
            )
        }

    }

    /**
     * 海报
     */
    private suspend fun setClassifyBanner() {
        // 发起网络请求获取海报
        api.getRcClassifyBanner(getToken()).apply {
            // 判断是否获取成功！
            if (this.code == 200) {
                val data = this.data
                runOnUiThread {
                    rcClassifyBanner.apply {
                        this.adapter =
                            object : BannerImageAdapter<RcClassifyBanner.DataBean>(data) {
                                override fun onBindView(
                                    p0: BannerImageHolder?,
                                    p1: RcClassifyBanner.DataBean?,
                                    p2: Int,
                                    p3: Int
                                ) {
                                    glide(url + p1?.imgUrl, p0?.imageView!!)
                                }
                            }
                    }
                }
            } else runOnUiThread { this.msg?.show() }
        }
    }


    /**
     * 常见分来垃圾
     */
    private suspend fun setRcClassifyAll(){
        api.getRcClassifyAll(getToken()).apply {
            if (this.code == 200){
                runOnUiThread {
                    val rows = this.rows
                    rcClassifyAllRv.apply {
                        this.isNestedScrollingEnabled = false
                        this.layoutManager = GridLayoutManager(context, 5)
                        this.adapter = RcClassifyAllTitleAdapter(ArrayList<RcClassifyAllModel.RowsBean>().apply {
                            this.addAll(rows!!)
                        }, R.layout.re_rc_seek_show_all_title)
                    }
                }
            }else runOnUiThread { this.msg?.show() }
        }
    }

    // 热搜适配器
    inner class RcClassifyAllTitleAdapter(
        val list: MutableList<RcClassifyAllModel.RowsBean>,
        layoutId: Int
    ) : BaseRecyclerAdapter<RcClassifyAllModel.RowsBean>(list, layoutId) {
        override fun setData(
            view: BaseViewHolder.ViewFind,
            data: RcClassifyAllModel.RowsBean,
            position: Int,
            holder: BaseViewHolder
        ) {
            val d = list[position]
            val fv = holder.viewFind
            val con = holder.viewFind.view?.context

            // 填充数据
            fv.rcSeekShowTitle.text = d.name

            holder.itemView.setOnClickListener {
                con?.startActivity(Intent(con , RcShowClassifyActivity::class.java).putExtra("type", d.type.toString()))
            }
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}